<?php

namespace App\Admin\Controllers;

use App\Models\OauthUser;
use App\Models\Tag;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;

class UserController extends AdminController
{

    public function index(Content $content)
    {
        return $content
            ->header(trans('admin.user'))
            ->description(trans('admin.list'))
            ->body($this->grid()->render());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new OauthUser());

        $grid->id('ID')->sortable();
        $grid->name('昵称');
        $grid->openid('昵称');
        $grid->email('邮箱');
        $grid->avatar('头像')->image();
	
	    $states = [
		    'on'  => ['value' => 1, 'text' => '正常'],
		    'off' => ['value' => 0, 'text' => '无效'],
	    ];
	    $grid->status('状态')->switch($states);
	    
        $grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableView();
        });

        $grid->tools(function (Grid\Tools $tools) {
            $tools->batch(function (Grid\Tools\BatchActions $actions) {
                $actions->disableDelete();
            });
        });
	    $grid->filter(function ($filter) {
		    $filter->column(1 / 2, function ($filter) {
			    $filter->where(function ($query) {
				
				    $query->where('name', 'like', "%{$this->input}%")
				    ->orWhere('email', 'like', "%{$this->input}%");
			    }, '关键词')->placeholder('关键词(包括昵称,邮箱)');
		    });
		
		    $filter->column(1 / 2, function ($filter) {
			    $filter->gt('created_at')->datetime();
			    $filter->between('updated_at')->datetime();
			    $filter->equal('status')->radio([
				                                    1 => '正常',
				                                    0 => '关闭',
			                                    ]);
		    });
	    });
        return $grid;
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header(trans('admin.user'))
            ->description(trans('admin.create'))
            ->body($this->form());
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    public function form()
    {
        $user = new OauthUser();

        $form = new Form($user);

        $form->display('id', 'ID');

        $form->text('name', '昵称')->rules('required');
//        $form->text('name', '昵称')->rules('required');
//        $form->text('name', '昵称')->rules('required');
	    $states = [
		    'on'  => ['value' => 1, 'text' => '正常', 'color' => 'success'],
		    'off' => ['value' => 0, 'text' => '无效', 'color' => 'danger'],
	    ];
	    $form->switch('status', '状态')->states($states);
        $form->display('created_at', trans('admin.created_at'));
        $form->display('updated_at', trans('admin.updated_at'));

        return $form;
    }

    /**
     * Edit interface.
     *
     * @param $id
     *
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header(trans('admin.user'))
            ->description(trans('admin.edit'))
            ->body($this->form()->edit($id));
    }

    /**
     * Show interface.
     *
     * @param mixed   $id
     * @param Content $content
     *
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header(trans('admin.user'))
            ->description(trans('admin.detail'))
            ->body($this->detail($id));
    }

}
